package com.tdwyz.fictionmanage.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tdwyz.fictionmanage.mapper.FictionMapper;
import com.tdwyz.fictionmanage.pojo.Fiction;
import com.tdwyz.fictionmanage.pojo.PageBean;
import com.tdwyz.fictionmanage.pojo.SensitiveWord;
import com.tdwyz.fictionmanage.utils.SensitiveWordUtil;
import com.tdwyz.fictionmanage.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FictionService {
    @Autowired
    private SensitiveWordService wordService;
    @Resource
    private FictionMapper fictionMapper;

    public boolean update(Fiction fiction) {
        int userId = ThreadLocalUtil.getUserId();
        //设置创建人
        fiction.setCreateUser(userId);

        //审核内容
        handlerSensitiveWordScan(fiction);
        //调用数据层插入数据
        return fictionMapper.update(fiction);
    }

    public Fiction getById(Integer id) {
        return fictionMapper.getById(id);
    }


    public boolean add(Fiction fiction) {
        int userId = ThreadLocalUtil.getUserId();

        //审核内容
        handlerSensitiveWordScan(fiction);
        //设置创建人
        fiction.setCreateUser(userId);
        //调用数据层插入数据
        return fictionMapper.add(fiction);
    }

    private void handlerSensitiveWordScan(Fiction fiction){
        //1.获取词典
        List<SensitiveWord> sensitiveWords = wordService.list();
        //2.初始化词典
        SensitiveWordUtil.initMap(sensitiveWords.stream().map(SensitiveWord::getWord).collect(Collectors.toList()));
        //3.匹配文章和标题违禁词
        if(SensitiveWordUtil.matchWords(fiction.getTitle()).size()>0){
            throw new RuntimeException("标题存在违禁词");
        }
        if(SensitiveWordUtil.matchWords(fiction.getIntroduction()).size()>0){
            throw new RuntimeException("简介存在违禁词");
        }
    }

    public PageBean<Fiction> listMy(Integer pageNum, Integer pageSize, Integer categoryId) {
        //1.返回对象
        PageBean<Fiction> pageBean = new PageBean<>();

        //2.开启分页
        PageHelper.startPage(pageNum,pageSize);

        //3.归属问题
        Integer userId = ThreadLocalUtil.getUserId();
        List<Fiction> fictions = fictionMapper.listMy(userId,categoryId);

        //使用page
        Page<Fiction> page = (Page<Fiction>) fictions;

        //填充数据
        pageBean.setTotal(page.getTotal());
        pageBean.setData(page.getResult());
        return pageBean;
    }

    public PageBean<Fiction> list(Integer pageNum, Integer pageSize, Integer categoryId) {
        //1.返回对象
        PageBean<Fiction> pageBean = new PageBean<>();

        //2.开启分页
        PageHelper.startPage(pageNum,pageSize);

        List<Fiction> fictions = fictionMapper.list(categoryId);

        //使用page
        Page<Fiction> page = (Page<Fiction>) fictions;

        //填充数据
        pageBean.setTotal(page.getTotal());
        pageBean.setData(page.getResult());
        return pageBean;
    }

    public boolean delete(Integer id) {

        return fictionMapper.delete(id);
    }

}
